package com.crm.interceptor;

import com.crm.annotation.PassToken;
import com.crm.constant.BusinessException;
import com.crm.constant.dto.ResultCode;
import com.crm.utils.JwtUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @version 1.0
 * @author: CRM_SYSTEM
 * @date: 2022/3/29 21:16
 * @Description:
 */
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//        if (1==1){
//            return true;
//        }

        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }

        String requestURI = request.getRequestURI();
        String token = request.getHeader("token");

        if (token == null) {
            throw new BusinessException(ResultCode.EXCEPTION_TOKEN.getCode(),ResultCode.EXCEPTION_TOKEN.getMessage());
        }

       if ( JwtUtils.verify(token)){
           return true;
       }else {
           throw new  BusinessException(ResultCode.EXCEPTION_TOKEN.getCode(),ResultCode.EXCEPTION_TOKEN.getMessage());
       }

//        String userId;
//        try {
//            userId = JWT.decode(token).getAudience().get(0);
//        } catch (JWTDecodeException e) {
//            throw new BusinessException(ResultCode.EXCEPTION_TOKEN.getCode(),ResultCode.EXCEPTION_TOKEN.getMessage());
//        }
//
////        User user = userService.findUserById(userId);
////        User user1=new User("12","123","123");
//
////        if (user==null){
////          throw new RuntimeException("用户不存在，请重新登录");
////        }
////        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();
//        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("123")).build();
//        try {
//            jwtVerifier.verify(token);
//        } catch (JWTVerificationException e) {
//            throw new BusinessException(ResultCode.EXCEPTION_TOKEN.getCode(),ResultCode.EXCEPTION_TOKEN.getMessage());
//        }
//        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {

    }
}
